{include file="common/comm_header" /}

<body>
{if condition="empty($class_data)"}
<div class="x-body layui-anim layui-anim-up">
    <h1>出现异常</h1>
</div>
{else /}
<div class="x-body layui-anim layui-anim-up">
    <input type="hidden" readonly="readonly" id="init_data" value={$json_class_data}>
    <form class="layui-form">
        <input type="hidden" name="class_id" value="{$class_data.assort_id}">
        <div class="layui-form-item">
            <label for="L_class_title" class="layui-form-label">
                <span class="x-red">*</span>
                分类标题：
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_class_title" name="class_title" value="{$class_data.assort_title}" required="" lay-verify="class_title"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                分类标题不能为空
            </div>
        </div>

        <div class="layui-form-item">
            <label for="L_class_description" class="layui-form-label">
                分类描述：
            </label>
            <div class="layui-input-inline">
                <textarea style="resize: none;"  id="L_class_description" name="class_description" placeholder="可不写，也可随便写写" class="layui-textarea">{$class_data.assort_description}</textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="L_class_sort" class="layui-form-label">
                分类排序：
            </label>
            <div class="layui-input-inline">
                <input type="text" id="L_class_sort" name="class_sort" value="{$class_data.assort_sort}" value="0" required="" lay-verify="class_sort"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                -999到999区间的任意两位小数
            </div>
        </div>

        <div class="layui-form-item">
            <label for="L_class_status" class="layui-form-label">
                分类状态：
            </label>
            <div class="layui-input-inline">
                <input id="L_class_status" type="radio" name="class_status" value="1" title="有效" {eq name="$class_data.assort_status" value="1"}checked{/eq}>
                <input type="radio" name="class_status" value="0" title="无效" {eq name="$class_data.assort_status" value="0"}checked{/eq}>
            </div>
        </div>






        <!--提交按钮-->
        <div class="layui-form-item">
            <label for="L_class_sort" class="layui-form-label">
            </label>
            <button id="addAssort"  class="layui-btn" lay-filter="edit" lay-submit="">
                修改
            </button>
        </div>
    </form>
</div>
<script type="text/javascript" src="/static/common/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/admin//lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="/static/admin//js/xadmin.js"></script>
<!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
<!--[if lt IE 9]>
<script src="/static/common/js/html5.min.js"></script>
<script src="/static/common/js/respond.min.js"></script>
<![endif]-->
<script>
    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
                ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            class_title: function(value){
                var valLen = value.length;
                if(valLen < 1){
                    return '分类标题不能为空啊';
                }
                if(valLen >40){
                    return '分类标题也太长了吧，不要超过40字符';
                }
            }
            ,class_sort: function(value){
                var reg = /^-?\d+\.?\d{0,2}$/;
                if(!(reg.test(value))){
                    return  '排序只能是两位小数的数字啊';
                }
                if(value < -999 || value > 999){
                    return '排序只能在999到-999的区间';
                }
            }
        });

        //监听提交
        form.on('submit(edit)', function(data){
            ///获取表单数据
            var post_data = {class_id:parseFloat(data.field.class_id),class_title:data.field.class_title,class_description:data.field.class_description,class_sort:data.field.class_sort,class_status:parseFloat(data.field.class_status)};
            ///比较旧数据
            if($('#init_data').val() == JSON.stringify(post_data)){
                layer.alert('您可是什么都没操作啊！', {
                    icon: 5,
                    time: 2000
                });
                return false;
            }
            ///提交修改
            $('#addAssort').attr('disabled',true);
            $.ajax({
                url:'/{$Think.ADMIN_ROUT_PREFIX}/assort-edit/'+Math.random(),
                timeout : 1000,
                type : 'post',
                data :post_data,
                async: false,
                dataType:'json',
                success:function(data){
                    var testJson = $.parseJSON(data);
                    if(testJson.status == 0){
                        layer.alert(testJson.msg, {
                            icon: 6,
                            time: 1000,
                            closeBtn:0,
                            end:function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                            }
                        });
                    }else if(testJson.status == 2){
                        layer.alert(testJson.msg, {
                            icon: 5,
                            time: 2000
                        });
                        $('#addAssort').attr('disabled',false);
                        return false;
                    }else{
                        layer.alert('出现异常', {
                            icon: 5,
                            time: 2000
                        });
                        $('#addAssort').attr('disabled',false);
                        return false;
                    }
                },
                error:function(){
                    layer.alert('出现异常，建议刷新页面。', {
                        icon: 5,
                        time: 2000
                    });
                    $('#addAssort').attr('disabled',false);
                    return false;
                }
            });

            return false;
        });


    });
</script>
{/if}
</body>

{include file="common/comm_bottom" /}
